﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using IDAL;
using System.Data.SqlClient;
using System.Data;
using Newtonsoft.Json;
namespace DAL
{
   public class Manager_DAL:IManager
   {
       SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MengQiqidb;Integrated Security=True");//数据库连接对象

       #region 添加供应商基本信息
       public int Add(Manager_tb model)
       {
           SqlCommand cmd = new SqlCommand("p_addManagerData", conn);//创建命令对象
           cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
           SqlParameter[] pars = { 
                                  new SqlParameter("@mgName",model.mgName),
                                  new SqlParameter("@Password",model.Password),
                                  new SqlParameter("@Permitted",model.Permitted),
                                  };//传递参数
           cmd.Parameters.AddRange(pars);//添加到命令对象
           try//异常处理机制
           {
               conn.Open();//打开数据库
               int result = cmd.ExecuteNonQuery();//返回受影响行数
               return result;
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               conn.Close();//关闭数据库
               conn.Dispose();//释放内存
           }
        }
       #endregion 

       #region 修改供应商基本信息
       public int Upt(Manager_tb model)
        {
            SqlCommand cmd = new SqlCommand("p_uptManagerData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            SqlParameter[] pars = { 
                                  new SqlParameter("@mgName",model.mgName),
                                  new SqlParameter("@Password",model.Password),
                                  new SqlParameter("@Permitted",model.Permitted),
                                  new SqlParameter("@mgNo",model.mgNo),
                                  };//传递参数
            cmd.Parameters.AddRange(pars);//添加到命令对象
            try//异常处理机制
            {
                conn.Open();//打开数据库
                int result = cmd.ExecuteNonQuery();//返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
       #endregion

       #region 删除供应商基本信息
       public int Del(object id)
        {
            SqlCommand cmd = new SqlCommand("p_delManagerData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            SqlParameter[] pars = { 
                                  new SqlParameter("@mgNo",id),
                                  };//传递参数
            cmd.Parameters.AddRange(pars);//添加到命令对象
            try//异常处理机制
            {
                conn.Open();//打开数据库
                int result = cmd.ExecuteNonQuery();//返回受影响行数
                return result;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
       #endregion

       #region 获取管理员基本信息
       public List<Manager_tb> GetData()
        {
            SqlCommand cmd = new SqlCommand("p_getManagerData", conn);//创建命令对象
            cmd.CommandType = CommandType.StoredProcedure;//声明存储过程
            try//异常处理机制
            {
                conn.Open();//打开数据库
                DataTable dt = new DataTable();
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.Fill(dt);//填充数据
                List<Manager_tb> list = JsonConvert.DeserializeObject<List<Manager_tb>>(JsonConvert.SerializeObject(dt));
                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();//关闭数据库
                conn.Dispose();//释放内存
            }
        }
       #endregion
   }
}
